package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.fragment.order.UserOrderFragment;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 用户订单界面
 */
public class UserOrderActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_order_all)
    TextView btn_all;
    @BindView(R.id.tv_order_fahuo)
    TextView btn_daifahuo;
    @BindView(R.id.tv_order_shouhuo)
    TextView btn_daishouhuo;
    @BindView(R.id.tv_order_end)
    TextView btn_complete;
    @BindView(R.id.ll_order_state)
    AutoLinearLayout llOrderState;
    @BindView(R.id.tv_order_daifukaun)
    TextView btn_daifukaun;

    private ArrayList<View> bottomNavs;
    private FragmentManager fragmentManager;
    private UserOrderFragment allFragment;   //全部
    private UserOrderFragment daiFuKuanFragment; //待付款
    private UserOrderFragment daiFaHuoFragment;  //待发货
    private UserOrderFragment daiShouHuoFragment;  //待收货
    private UserOrderFragment completeFragment;  //完成

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_order);
        ButterKnife.bind(this);
        initView();
        //默认全部被选中
        fragmentManager = this.getSupportFragmentManager();
        clickPosition(0);
        creatFragment(0);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(getResources().getString(R.string.my_order));

        bottomNavs = new ArrayList<>();
        for (int i = 0; i < llOrderState.getChildCount(); i++) {
            View child = llOrderState.getChildAt(i);
            child.setTag(i);
            bottomNavs.add(child);
        }

    }

    private void reset() {
        for (View v : bottomNavs) {
            //v.setSelected(false);
            TextView textView = (TextView) v;
            textView.setTextColor(ContextCompat.getColor(this, R.color.all_textcolor));
        }
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_order_daifukaun, R.id.tv_order_all, R.id.tv_order_fahuo,
            R.id.tv_order_shouhuo, R.id.tv_order_end})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.tv_order_all:
                clickPosition(0);
                creatFragment(0);
                break;
            case R.id.tv_order_daifukaun:
                clickPosition(1);
                creatFragment(1);
                break;
            case R.id.tv_order_fahuo:
                clickPosition(2);
                creatFragment(2);
                break;
            case R.id.tv_order_shouhuo:
                clickPosition(3);
                creatFragment(3);
                break;
            case R.id.tv_order_end:
                clickPosition(4);
                creatFragment(4);
                break;
        }
    }

    /**
     * 点击位置
     */
    private void clickPosition(int i) {
        reset();
        switch (i) {
            case 0:
                btn_all.setTextColor(ContextCompat.getColor(this, R.color.main_theme_color));
                break;
            case 1:
                btn_daifukaun.setTextColor(ContextCompat.getColor(this, R.color.main_theme_color));
                break;
            case 2:
                btn_daifahuo.setTextColor(ContextCompat.getColor(this, R.color.main_theme_color));
                break;
            case 3:
                btn_daishouhuo.setTextColor(ContextCompat.getColor(this, R.color.main_theme_color));
                break;
            case 4:
                btn_complete.setTextColor(ContextCompat.getColor(this, R.color.main_theme_color));
                break;
        }

    }

    private void creatFragment(int position) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
//        1未付款 2待发货 3待收货4全部 5 已收货（完成）
        switch (position) {
            case 0:  //全部
                if (allFragment == null) {
                    allFragment = new UserOrderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("loadType", Constant.ORDER_ALL); //type=0  全部
                    allFragment.setArguments(bundle);
                    transaction.add(R.id.user_order_container, allFragment);
                } else {
                    if (!allFragment.isVisible()) {
                    }
                    transaction.show(allFragment);
                }
                break;
            case 1: //待付款
                if (daiFuKuanFragment == null) {
                    daiFuKuanFragment = new UserOrderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("loadType", Constant.ORDER_PAY); //type=1 待付款
                    daiFuKuanFragment.setArguments(bundle);
                    transaction.add(R.id.user_order_container, daiFuKuanFragment);
                } else {
                    if (!daiFuKuanFragment.isVisible()) {
                    }
                    transaction.show(daiFuKuanFragment);
                }
                break;

            case 2:   //type=2  待发货
                if (daiFaHuoFragment == null) {
                    daiFaHuoFragment = new UserOrderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("loadType", Constant.ORDER_SEND);
                    daiFaHuoFragment.setArguments(bundle);
                    transaction.add(R.id.user_order_container, daiFaHuoFragment);
                } else {
                    if (!daiFaHuoFragment.isVisible()) {
                    }
                    transaction.show(daiFaHuoFragment);
                }
                break;
            case 3://type=3 待收货
                if (daiShouHuoFragment == null) {
                    daiShouHuoFragment = new UserOrderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("loadType", Constant.ORDER_RECE);
                    daiShouHuoFragment.setArguments(bundle);
                    transaction.add(R.id.user_order_container, daiShouHuoFragment);
                } else {
                    if (!daiShouHuoFragment.isVisible()) {
                    }
                    transaction.show(daiShouHuoFragment);
                }
                break;
            case 4://type=4 完成
                if (completeFragment == null) {
                    completeFragment = new UserOrderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("loadType", Constant.ORDER_FINISH);
                    completeFragment.setArguments(bundle);
                    transaction.add(R.id.user_order_container, completeFragment);
                } else {
                    if (!completeFragment.isVisible()) {
                    }
                    transaction.show(completeFragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();

    }

    /**
     * 隐藏fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (allFragment != null) {
            transaction.remove(allFragment);
            allFragment=null;
        }
        if (daiFuKuanFragment != null) {
            transaction.remove(daiFuKuanFragment);
            daiFuKuanFragment = null;
        }
        if (daiFaHuoFragment != null) {
            transaction.remove(daiFaHuoFragment);
            daiFaHuoFragment = null;
        }
        if (daiShouHuoFragment != null) {
            transaction.remove(daiShouHuoFragment);
            daiShouHuoFragment = null;
        }
        if (completeFragment != null) {
            transaction.remove(completeFragment);
            completeFragment = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        allFragment = null;
        daiFuKuanFragment = null;
        daiShouHuoFragment = null;
        daiFaHuoFragment = null;
        completeFragment = null;
    }

    //--------------------------------------------
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getInfo(String string) {
        switch (string) {
            case "end":
                daiFuKuanFragment.notifyView();
                break;
        }
    }


    //--------------------------------------------


    //-----------------------------------------------------------
}
